#!/usr/bin/python
# -*- coding: utf-8 -*-

#导报
import smtplib, time, os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

#定义一个发送邮件的类
class SendEmail(object):
    #初始化方法
    def __init__(self):
        #配置邮箱属性
        #发送邮箱
        self.sender = '1033353613@qq.com'
        #接收邮箱
        self.receiver = ['1033353613@qq.com', '979034018@qq.com']
        #发送邮件主题
        t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
        self.subject = '自动化测试结果_' + t
        #发送邮件服务器
        self.smtpserver = 'smtp.qq.com'
        #发送邮箱用户/密码
        self.username = '1033353613@qq.com'
        # 注意：如果邮箱从未开启过pop3/smtp服务，则需要进入邮箱设置开启，开启成功后会提示一个授权码，把此处的密码替换为生成的授权码即可
        self.password = 'kptqxaqnhsmpbdjc'
        # password = 'xxxx'
        # 邮件内容
        # self.content = 'Python 邮件发送测试...'

    def email_content(self, file):
        # 第二步： 组装邮件内容和标题，中文需参数‘utf-8’，单字节字符不需要
        # self.msg = MIMEText(self.content, 'plain', 'utf-8')
        # self.msg['Subject'] = Header(self.subject, 'utf-8')
        # self.msg['From'] = self.sender
        # self.msg['To'] = self.receiver
        #获取测试报告目录
        # file = os.path.dirname(os.path.dirname(__file__)) + r'/testReport'
        # #获取目录下所有邮件
        # filr_list = os.listdir(file)
        # print(filr_list)
        # 读取html文件内容
        # with open(file, 'rb') as f:
        #     mail_body = f.read()
        #     # 组装邮件内容和标题
        #     msg = MIMEMultipart()
        #     # 添加附件内容
        #     att = MIMEText(mail_body, 'plain', 'utf-8')
        #     att["Content-Type"] = 'application/octet-stream'
        #     att["Content-Disposition"] = 'attachment; filename=report.html'
        #     msg.attach(att)
        #     # 添加邮件的文本内容
        #     content = '自动化测试报告详情，请查收附件'
        #     msg.attach(MIMEText(content, 'plain', 'utf-8'))
        #     msg['Subject'] = Header(subject, 'utf-8')
        #     msg['From'] = sender
        #     msg['To'] = receiver
        #获取最新的html文件
        #   读取html文件
        with open(file, 'rb') as f:
            mail_body = f.read()
            # 组装邮件内容和标题
            self.msg = MIMEMultipart()
            # 添加附件内容
            att = MIMEText(mail_body, 'plain', 'utf-8')
            att["Content-Type"] = 'application/octet-stream'
            att["Content-Disposition"] = 'attachment; filename=report.html'
            self.msg.attach(att)
            # 添加邮件的文本内容
            content = '自动化测试报告详情，请查收附件'
            self.msg.attach(MIMEText(content, 'plain', 'utf-8'))
            self.msg['Subject'] = Header(self.subject, 'utf-8')
            self.msg['From'] = self.sender
            self.msg['To'] = ",".join(self.receiver)

        # 第三步：登录并发送邮件
    def send_email(self, file):
        self.email_content(file)
        try:
            # 1--实例化smtp类
            smtp = smtplib.SMTP()
            # 2--连接stmp服务器
            smtp.connect(self.smtpserver)
            # 3--登录邮箱
            smtp.login(self.username, self.password)
            # 4--设置发件人，收件人，邮件内容
            smtp.sendmail(self.sender, self.receiver, self.msg.as_string())
        except Exception as msg:
            print("邮件发送失败！%s" % msg)
        else:
            print("邮件发送成功！")
        finally:
            smtp.quit()

if __name__ == '__main__':
    # 调试发送邮件
    path = r'E:\project\vip20_framework\testReport\2022_29_05_11_51_00report.html'
    result = SendEmail()
    result.send_email(path)
